<?php

namespace app\index\controller;

use think\Controller;
use think\Request;
use think\Db;
use think\Session;
use think\Image;
class Carousel extends Controller
{
	// 主页显示
	public function index()
	{

		$file = Db::name('carousel c')
		->join('article a','a.aid = c.aid')
		->order('display desc')
		->select();
		// var_dump($file);
		return view('Carousel/index',[
			'file' => $file,
			]);
	}
	//新闻选择
	public function add()
	{	
		$list = Db::name('article')->select();
		// var_dump($list);die;
		return view('Carousel/addCarousel',[
			'list' => $list,
			]);
	}
	//添加
	public function adds($aid)
	{
		$list = Db::name('article')->where('aid',$aid)->select();
		// $p = $list[0]['amage'];
		// $p = ltrim($p);
		// var_dump($p);die;

		$image = Image::open('./'.$list[0]['amage']);
		if (!is_dir('static/uploads'.DS.date('Ymd',time()))) {
			mkdir('static/uploads/'.date('Ymd',time()));
		} 
			$file = 'static/uploads/'.date('Ymd',time()).'/'.rand().'.jpg';
		
		
		$img = $image->thumb(664, 300,6)->save($file);

		$data = ['aid' => $list[0]['aid'], 'cmage' => $file];
		$list = Db::name('carousel')->insert($data);

		if ($list) {
		return $this->success('添加成功',url('index/Carousel/index'));
		} else {
		return $this->error('添加失败');

		}
    }    
    //删除
    public function delete($aid)
    {
    	$list = Db::name('Carousel')->delete($aid);
    	return json($list);
    }
    //封面
    public function seal($id)
    {	
    	$ll = Db::name('Carousel')->field('id')->select();
		foreach ($ll as $key => $value) {
			$ll = $value['id'];
			$mu = Db::name('Carousel')->where('id',$ll)->update(['home'=>'1']);

		}
    	$list = Db::name('Carousel')->where('id',$id)->setField('home' , '0');
    	if ($list) {
    		return $this->success('封面设置成功',url('index/Carousel/index'));
    	} else {
    		return $this->error('封面设置失败',url('index/Carousel/index'));	
    	}
    }
    //非封面
    public function seall($id)
    {	
    	$list = Db::name('Carousel')->where('id',$id)->setField('home' , '1');
    	if ($list) {
    		return $this->success('非封面设置成功',url('index/Carousel/index'));
    	} else {
    		return $this->error('非封面设置失败',url('index/Carousel/index'));	
    	}
    	
    }
 	//显示
    public function play($id)
    {	
    	$list = Db::name('Carousel')->where('id',$id)->setField('display' , '1');
    	return json($list);	
    }
	//不显示
    public function playn($id)
    {	
    	$list = Db::name('Carousel')->where('id',$id)->setField('display' , '0');
    	return json($list);	
    }

}